package com.cheney.demo.easypoi;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chensi
 * @date 2022/3/16 14:01
 */
public class 导出自定义模板生成的文件 {

    @Test
    public void testExport() throws Exception {
        TemplateExportParams params = new TemplateExportParams(
                "doc/互联网团体意外伤害保险增员excel模板.xlsx", 0);
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("ID", "001");
        map.put("name", "老赵");
        map.put("ID_type", "身份证");
        map.put("ID_number", "32091111111111");
        map.put("Date_of_birth", "1988-12-31");
        map.put("Scheme_name", "计划一（职业类别1）：年计划");
        map.put("Date_start", "2000-12-31");
        map.put("Date_end", "2018-12-31");
        map.put("Branch", "1");
        map.put("Remarks", "1");

        for (int i = 0; i < 18; i++) {
            list.add(map);
        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", list);
        Workbook book = ExcelExportUtil.exportExcel(params, resultMap);
        File savefile = new File("D:/data/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("D:/data/excel/互联网团体意外伤害保险增员excel模板.xlsx");
        book.write(fos);
        fos.close();

//        @Cleanup ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
//        book.write(os);
//        byte[] bytes = os.toByteArray();
//        String base64Str = "data:application/xlsx;base64," + Base64.encode(bytes);
//        System.out.println(base64Str);

    }
}
